# !/usr/bin/env python
# -*- coding: utf-8 -*-
# author: luhx
# https://pypi.org/project/websocket-client/
# pip install websocket-client
# 火币数据调试， 感觉这个websocket客户端很好用

# from websocket import create_connection
# import gzip
# import time
#
# if __name__ == '__main__':
#     while(1):
#         try:
#             ws = create_connection("wss://api.huobipro.com/ws")
#             break
#         except:
#             print('connect ws error,retry...')
#             time.sleep(5)
#
#     # 订阅 KLine 数据
#     tradeStr="""{"sub": "market.ethusdt.kline.1min","id": "id10"}"""
#
#     ws.send(tradeStr)
#     while(1):
#         compressData=ws.recv()
#         result=gzip.decompress(compressData).decode('utf-8')
#         if result[:7] == '{"ping"':
#             ts=result[8:21]
#             pong='{"pong":'+ts+'}'
#             ws.send(pong)
#             ws.send(tradeStr)
#         else:
#             print(result)


# from websocket import create_connection
# import gzip
# import time
#
# if __name__ == '__main__':
#     while(1):
#         try:
#             ws = create_connection("wss://api.huobipro.com/ws")
#             break
#         except:
#             print('connect ws error,retry...')
#             time.sleep(5)
#
#     # 订阅 KLine 数据
#     tradeStr="""{"sub": "market.ethusdt.kline.1min","id": "id10"}"""
#
#     # 请求 KLine 数据
#     # tradeStr="""{"req": "market.ethusdt.kline.1min","id": "id10", "from": 1513391453, "to": 1513392453}"""
#
#     #订阅 Market Depth 数据
#     # tradeStr="""{"sub": "market.ethusdt.depth.step5", "id": "id10"}"""
#
#     #请求 Market Depth 数据
#     # tradeStr="""{"req": "market.ethusdt.depth.step5", "id": "id10"}"""
#
#     #订阅 Trade Detail 数据
#     # tradeStr="""{"sub": "market.ethusdt.trade.detail", "id": "id10"}"""
#
#     #请求 Trade Detail 数据
#     # tradeStr="""{"req": "market.ethusdt.trade.detail", "id": "id10"}"""
#
#     #请求 Market Detail 数据
#     # tradeStr="""{"req": "market.ethusdt.detail", "id": "id12"}"""
#
#     ws.send(tradeStr)
#     while(1):
#         compressData=ws.recv()
#         result=gzip.decompress(compressData).decode('utf-8')
#         if result[:7] == '{"ping"':
#             ts=result[8:21]
#             pong='{"pong":'+ts+'}'
#             ws.send(pong)
#             ws.send(tradeStr)
#         else:
#             print


import websocket


def on_message(ws, message):  # 服务器有数据更新时，主动推送过来的数据
    print(message)


def on_error(ws, error):  # 程序报错时，就会触发on_error事件
    print(error)


def on_close(ws):
    print("Connection closed ……")


def on_open(ws):  # 连接到服务器之后就会触发on_open事件，这里用于send数据
    req = '{"event":"subscribe", "channel":"market.btcusdt.kline.1min"}'
    print(req)
    ws.send(req)


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("wss://api.huobi.pro/ws",
                                on_message=on_message,
                                on_error=on_error,
                                    on_close=on_close)
    ws.on_open = on_open
    ws.run_forever(ping_timeout=30)